<?php

namespace App\Http\Controllers\merchant;

use App\Http\Controllers\Controller;
use App\Models\Mysql\XnGoodsTimeIsUp;
use App\Models\Mysql\XnMerchant;
use App\Models\Mysql\XnMerchantGoodsInvoicing;
use App\Models\Mysql\XnMerchantGoodsTimeIsUp;
use App\Models\Mysql\XnSupplierGoods;
use App\Models\Mysql\XuGoodsBrand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Translation\t;

class MerchantGoodsController extends Controller
{
    //商户商品列表
    public function list(Request $request)
    {
        $params = $request->all();
        $where =[];
        if (!empty($params['goods_name'])){
            $where[] = ['goods_name','like','%'.$params['goods_name'].'%'];
        }
        if (!empty($params['brand_id'])) {
            $strs = 'pid';
            $b = $params['brand_id'];
        } else {
            $strs = 'level';
            $b = 3;
        }
        $brand_ids = [];
        $brandrealid =XuGoodsBrand::where($strs, $b)->select('category_id')->get();
        $brand_idinfo = json_decode(json_encode($brandrealid), true);
        foreach ($brand_idinfo as $k => $v) {
            $brand_ids[] = $v['category_id'];
        }
        $where[] = ['is_up',1];
        $res = XnSupplierGoods::where($where)
            ->orderBy('price','desc')
            ->wherein('release',[1,3])
            ->wherein('brand_id',$brand_ids)
            ->select('id as product_id','goods_name','price','release','goods_belong','goods_type','timing_is_up')
            ->paginate(10);
        foreach ($res as $k=>$v){
            if ($v['timing_is_up'] == 1){
                $TimingIsUp = XnGoodsTimeIsUp::where('goods_id',$v['product_id'])->first();
                $start_time = date('H:i:s', $TimingIsUp['shelftime']); //上架时间
                $end_time = date('H:i:s',$TimingIsUp['offshelftime']);//下架时间、、
                $res[$k]['TimingIsUp'] = $start_time.'--'.$end_time;
                $res[$k]['time_type'] = $TimingIsUp['type'];
            }
        }
        if (!$res){
            return $this->sendError('查询失败');
        }
       return $this->sendResponse($res,'查询成功');
    }
    //商户带票商品列表
    public function invoicing_list(Request $request){
        $params = $request->all();
        $user = auth('merchant')->user();
        $where =[];
        if (!empty($params['goods_name'])){
            $where[] = ['goods_name','like','%'.$params['goods_name'].'%'];
        }
        if (!empty($params['brand_id'])) {
            $strs = 'pid';
            $b = $params['brand_id'];
        } else {
            $strs = 'level';
            $b = 3;
        }
        $brand_ids = [];
        $brandrealid =XuGoodsBrand::where($strs, $b)->select('category_id')->get();
        $brand_idinfo = json_decode(json_encode($brandrealid), true);
        foreach ($brand_idinfo as $k => $v) {
            $brand_ids[] = $v['category_id'];
        }
        $where[] = ['is_up',1];
        $res = XnSupplierGoods::where($where)
            ->wherein('release',[1,3])
            ->wherein('brand_id',$brand_ids)
            ->orderBy('price','desc')
            ->select('id as product_id','goods_name','price','release','goods_belong','goods_type')
            ->paginate(10);
        foreach ($res as $k=>$v){
            $merchant = XnMerchantGoodsInvoicing::where(['merchant_num'=>$user->merchant_number,'goods_id'=>$v['product_id']])->first();
            $res[$k]['status'] = 2;
            if (!empty($merchant['price'])){
                $res[$k]['price'] = $merchant['price'];
                $res[$k]['status'] = 1;
            }
            if (!empty($merchant)&&$merchant['release'] == 1){
                if ($v['release'] ==3){
                    $res[$k]['release'] = 3;
                }else{
                    $res[$k]['release'] = 1;
                }
            }elseif ($v['release'] == 1 && empty($merchant)){
                $res[$k]['release'] = 1;
            }elseif(empty($merchant) || $merchant['release'] == 2 ){
                $res[$k]['release'] = 3;
            }
            if (empty($merchant['timing_is_up'])){
                $res[$k]['timing_is_up'] = 2;
            }else{
                $res[$k]['timing_is_up'] = $merchant['timing_is_up'];
            }

            if ($res[$k]['timing_is_up'] == 1){
                $TimingIsUp = XnMerchantGoodsTimeIsUp::where(['goods_id'=>$v['product_id'],'merchant_num'=>$user->merchant_number])->first();
                if (!empty($TimingIsUp)){
                    $start_time = date('H:i:s', $TimingIsUp['shelftime']); //上架时间
                    $end_time = date('H:i:s',$TimingIsUp['offshelftime']);//下架时间
                    $res[$k]['TimingIsUp'] = $start_time.'--'.$end_time;
                    $res[$k]['time_type'] = $TimingIsUp['type'];
                }
            }
        }
        if (!$res){
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');
    }
    //商户刷新余额
    public function refresh_balance(Request $request){
        $user = auth('merchant')->user();
        return $this->sendResponse(['balance'=>$user['balance']-$user['deduction_balance'],'merchant_balance_alarm'=>$user['merchant_balance_alarm']],'查询成功');
    }
    //商户开启余额报警
    public function Balance_alarm(Request $request){

        $params = $request->all();
        $user=  auth('merchant')->user();
        if (!$request->filled('alarm')){
            return $this->sendError('请输入余额阀值');
        }
        $res = XnMerchant::where('merchant_number',$user['merchant_number'])->update(['merchant_balance_alarm'=>$params['alarm']]);
        if (!$res){
            return  $this->sendError('操作失败');
        }
        return $this->sendResponse([],'操作成功');
    }
    //查询密钥
    public function check_key(Request $request){
        $params = $request->all();
        $user=  auth('merchant')->user();
        if (md5($params['transaction_password']) != $user['transaction_password']){
            return  $this->sendError('交易密码错误');
        }
        $data =[
            'merchant_num'=> $user['merchant_number'],
            'key'=>$user['secret_key']
        ];
        return $this->sendResponse($data,'查询成功');
    }
    //重置密钥
    public function resetting_key(){
        $user=  auth('merchant')->user();
        $set_key = password_hash(md5(19937776959), PASSWORD_DEFAULT);
        $res = XnMerchant::where('id',$user['id'])->update(['secret_key'=>$set_key]);
        if (!$res){
            return  $this->sendError('操作失败');
        }
        return $this->sendResponse($set_key,'操作成功');
    }
}
